import 'package:flutter/material.dart';
import 'package:toast/toast.dart';

/// BottomAppBar

class BottomAppBarRoute extends StatefulWidget {
  @override
  _BottomAppBarrRouteState createState() => _BottomAppBarrRouteState();
}

class _BottomAppBarrRouteState extends State<BottomAppBarRoute>
    with SingleTickerProviderStateMixin {
 // int _selectedIndex = 0;

  List _tabs = ["新闻", "历史", "图片"];

  //定义一个TabController
  TabController _tabController;

  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: _tabs.length, vsync: this);
    /* _tabController.addListener(() {
      switch (_tabController.index) {
        case 1:
          print("======$_tabController.index");
          break;
        case 2:
          print("======$_tabController.index");
          break;
        default:
          break;
      }
    });*/
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("App Name"),
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.share),
            onPressed: () => {
              Toast.show("导航分享", context),
            },
          ),
          Builder(builder: (context) {
            return IconButton(
              icon: Icon(Icons.mail_outline),
              onPressed: () => {
                //打开右侧抽屉菜单
                Scaffold.of(context).openEndDrawer(),
              },
            );
          })
        ],
        //顶部tabBar
        bottom: TabBar(
            indicatorColor: Colors.white,
            isScrollable: false,
            onTap: (index) => {Toast.show("tabBar$index", context)},
            controller: _tabController,
            tabs: _tabs
                .map((e) => Tab(text: e, icon: Icon(Icons.thumb_up)))
                .toList()),
      ),
      bottomNavigationBar: BottomAppBar(
        color: Colors.white,
        // 底部导航栏打一个圆形的洞
        shape: CircularNotchedRectangle(),
        child: Row(
          //均分底部导航栏横向空间
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: <Widget>[
            IconButton(
              icon: Icon(Icons.home),
              onPressed: ()=>{},
            ),
            SizedBox(), //中间位置
            IconButton(
              icon: Icon(Icons.business),
              onPressed: ()=>{},
            ),
          ],
        ),
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
      floatingActionButton: FloatingActionButton(
        //悬浮按钮
        child: Icon(Icons.add),
        onPressed: () {},
      ),
      //抽屉菜单
      drawer: _MyDrawer("左抽屉", 1),
      endDrawer: _MyDrawer("右抽屉", 2),
      body: TabBarView(
        controller: _tabController,
        children: _tabs.map((e) {
          return Container(
            alignment: Alignment.center,
            child: Text(e, textScaleFactor: 5),
          );
        }).toList(),
      ),
    );
  }
}

class _MyDrawer extends StatelessWidget {
  final String info;
  final int flag;

  _MyDrawer(this.info, this.flag);

  @override
  Widget build(BuildContext context) {
    return Drawer(
        child: MediaQuery.removePadding(
      //移除抽屉菜单顶部默认留白
      removeTop: true,
      context: context,
      child: Column(
        children: <Widget>[
          CircleAvatar(
            backgroundImage: NetworkImage(
                "https://avatars2.githubusercontent.com/u/20411648?s=460&v=4"),
          ),
          ClipOval(
            child: Image.network(
              "https://avatars2.githubusercontent.com/u/20411648?s=460&v=4",
              width: 120.0,
            ),
          ),
          Text(info),
          Expanded(
            child: ListView(
              children: <Widget>[
                ListTile(
                  leading: const Icon(Icons.add),
                  title: const Text("add account"),
                ),
                ListTile(
                  leading: const Icon(Icons.settings),
                  title: const Text("manage accounts"),
                ),
              ],
            ),
          )
        ],
      ),
    ));
  }

  BorderRadiusGeometry _getBorderRadius() {
    if (flag == 1) {
      return BorderRadius.only(
          topRight: Radius.circular(20.0), bottomRight: Radius.circular(20.0));
    } else {
      return BorderRadius.only(
          topLeft: Radius.circular(20.0), bottomLeft: Radius.circular(20.0));
    }
  }
}
